﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using GameEngine.Managers.IManagers;
using PvT.Entities;
using Microsoft.Xna.Framework.Graphics;
using GameEngine.Scenes;
using GameEngine.EntityComponents;
using GameEngine.Camera;
using PvT.Scenes.Factory;
using GameEngine.Utils;

namespace PvT.Scenes.Screens
{
    public class MainMenuScreen : AbstractGameScreen
    {
        private Texture2D logoTexture;

        public MainMenuScreen(Game game)
            : base(game)
        {
            this.MenuTitle = "";
            logoTexture = game.Content.Load<Texture2D>("logo");

            // Create our menu entries.
            MenuEntry playGameMenuEntry = new MenuEntry(game, "Play Game");
            MenuEntry exitMenuEntry = new MenuEntry(game, "Exit");

            // Hook up menu event handlers.
            playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
            exitMenuEntry.Selected += OnCancel;

            // Add entries to the menu.
            MenuEntries.Add(playGameMenuEntry);
            MenuEntries.Add(exitMenuEntry);
        }

        #region Draw

        /// <summary>
        /// Draws the menu.
        /// </summary>
        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            // make sure our entries are in the right place before we draw them
            UpdateMenuEntryLocations(225f);

            spriteBatch.Begin();

            // Draw each menu entry in turn.
            for (int i = 0; i < MenuEntries.Count; i++)
            {
                MenuEntry menuEntry = MenuEntries[i];
                bool isSelected = isActive && (i == selectedEntry);
                menuEntry.Draw(this, isSelected, gameTime, spriteBatch);
            }
                        
            Vector2 textureOrigin = new Vector2(logoTexture.Width, logoTexture.Height) / 2;
            Vector2 texturePosition = new Vector2(game.GraphicsDevice.Viewport.Width / 2, 120);

            spriteBatch.Draw(logoTexture, texturePosition, null, Color.White, 0f, textureOrigin, 1f, SpriteEffects.None, 0f);

            spriteBatch.End();
        }

        #endregion
        #region Handle Input

        /// <summary>
        /// Event handler for when the Play Game menu entry is selected.
        /// </summary>
        void PlayGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ISceneManager sceneManager = (ISceneManager)game.Services.GetService(typeof(ISceneManager));
            this.isActive = false;
            sceneManager.ClearScenes();
            Scene sceneWrapper;
            (new ConcreteFactory()).GetLevel(game, "", out sceneWrapper);
            sceneManager.PushScene(sceneWrapper);
        }

        /// <summary>
        /// Event handler for when the Options menu entry is selected.
        /// </summary>
        void OptionsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            //ScreenManager.AddScreen(new OptionsMenuScreen(), e.PlayerIndex);
        }

        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            game.Exit();
        }

        #endregion
    }
}
